package com.github.niefy.modules.wx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.niefy.common.exception.RRException;
import com.github.niefy.modules.wx.dao.WeiboCallMeMapper;
import com.github.niefy.modules.wx.entity.WeiboCallMe;
import com.github.niefy.modules.wx.enums.CommonYesNoEnum;
import com.github.niefy.modules.wx.service.WeiboCallMeService;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class WeiBoCallMeServiceImpl extends ServiceImpl<WeiboCallMeMapper, WeiboCallMe> implements WeiboCallMeService {

    @Override
    public void saveService(WeiboCallMe input) {
        checkInput(input);
        WeiboCallMe db = getByImplIp(input.getImp(), input.getIp());
        if (ObjectUtils.isNotEmpty(db)) {
            return;
        }
        input.setStatus(CommonYesNoEnum.YES.getCode());
        input.setDelFlag(CommonYesNoEnum.YES.getCode());
        boolean save = super.save(input);
        if (save) {
            System.out.println("保存成共");
            return;
        }
        System.out.println("保存失败");
    }

    public void checkInput(WeiboCallMe input) {
        if (ObjectUtils.isEmpty(input)) {
            throw new RRException("入参不能为空");
        }
        if (ObjectUtils.isEmpty(input.getImp())) {
            throw new RRException("入参imp不能为空");
        }
        if (ObjectUtils.isEmpty(input.getIp())) {
            throw new RRException("入参ip不能为空");
        }
        if (ObjectUtils.isEmpty(input.getUa())) {
            throw new RRException("入参ua不能为空");
        }
    }

    @Override
    public WeiboCallMe getByImplIp(String imp, String ip) {
        if (ObjectUtils.isEmpty(imp)) {
            throw new RRException("入参imp不能为空");
        }
        if (ObjectUtils.isEmpty(ip)) {
            throw new RRException("入参ip不能为空");
        }

        LambdaQueryWrapper<WeiboCallMe> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(WeiboCallMe::getDelFlag, CommonYesNoEnum.YES.getCode());
        lambdaQuery.eq(WeiboCallMe::getImp, imp);
        lambdaQuery.eq(WeiboCallMe::getIp, ip);
        return getOne(lambdaQuery);
    }

    @Override
    public WeiboCallMe getByNewOne() {
        LambdaQueryWrapper<WeiboCallMe> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(WeiboCallMe::getDelFlag, CommonYesNoEnum.YES.getCode());
        lambdaQuery.eq(WeiboCallMe::getStatus, CommonYesNoEnum.YES.getCode());
        lambdaQuery.orderByDesc(WeiboCallMe::getId);
        lambdaQuery.last(" limit 1 ");
        return getOne(lambdaQuery);
    }

    @Override
    public List<WeiboCallMe> listByIp(List<String> ipList) {
        if (ObjectUtils.isEmpty(ipList)) {
            return Lists.newArrayList();
        }

        LambdaQueryWrapper<WeiboCallMe> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(WeiboCallMe::getDelFlag, CommonYesNoEnum.YES.getCode());
        lambdaQuery.in(WeiboCallMe::getIp, ipList);
        List<WeiboCallMe> list = list(lambdaQuery);
        return ObjectUtils.isEmpty(list) ? Lists.newArrayList() : list;
    }

    @Override
    public WeiboCallMe disableService(Integer id) {
        WeiboCallMe byIdService = getById(id);
        if (Objects.isNull(byIdService)) {
            throw new RuntimeException("数据不存在");
        }
        byIdService.setStatus(CommonYesNoEnum.NO.getCode());
        super.updateById(byIdService);
        return byIdService;
    }
}
